﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommunergyLogic;
using System.Data.Linq;
using Communergy.Repository;
using Communergy.Repository.EntityFramework;


/// <summary>
/// Methods for creating new Manual data records
/// </summary>
public class ManualDataHelper
{


    /// <summary>
    /// Gets the most recent electricity_ manual reading for the specified meter.
    /// </summary>
    /// <param Name="meterId">The meter id.</param>
    /// <returns></returns>
    public static ElectricityDaily_Manual GetMostRecentElectricity_ManualReadingForMeter(int meterId)
    {

        var db = new communergyEntities();
        var meter = db.ElectricityMeter.Include("ElectricityDaily_Manual").Where(m => m.me_id == meterId).FirstOrDefault();
        var electricity = meter.ElectricityDaily_Manual.OrderByDescending(de => de.de_dateTime);

        var firstElectricity = electricity.FirstOrDefault();
        return firstElectricity;
    }


    public static List< ElectricityDaily_Manual> GetPastElectricity_ManualReadingForMeter(int meterId, DateTime dateFrom)
    {

        var db = new communergyEntities();
        var meter = db.ElectricityMeter.Include("ElectricityDaily_Manual").Where(m => m.me_id == meterId).FirstOrDefault();
        var electricity = meter.ElectricityDaily_Manual.Where(de=> de.de_dateTime >= dateFrom).OrderBy(de => de.de_dateTime).ToList();

       
        return electricity;
    }
    
    public static List<ElectricityDaily_Manual> CreateElectricityReadings(ElectricityDaily_Manual firstElectricity , DateTime readingDate, decimal kwhTotal, int meterId)
    {
        List<ElectricityDaily_Manual> newReadings = new List<ElectricityDaily_Manual>();
     
        DateTime lastReadingDate = firstElectricity != null ? firstElectricity.de_dateTime : readingDate.AddDays(-1);
        decimal lastKwhTotal = firstElectricity != null ? firstElectricity.de_kWhTotal.Value : kwhTotal;

        int readingDays = (int)readingDate.Subtract(lastReadingDate).TotalDays;
        decimal kwhIncrease = kwhTotal - lastKwhTotal;
        decimal dailyKwh = firstElectricity != null ? kwhIncrease / readingDays : 0;

        while (lastReadingDate < readingDate)
        {
            lastReadingDate = lastReadingDate.AddDays(1);
            lastKwhTotal += dailyKwh;
            var newReading = new ElectricityDaily_Manual
            {
                de_dateTime = lastReadingDate,
                de_kWhDaily = dailyKwh,
                de_kWhTotal = lastKwhTotal
            };
            
            newReadings.Add(newReading);
        }
        
        return newReadings;
    }

    public static void SaveNewElectricityReadings( communergyEntities db, ElectricityMeter meter, List<ElectricityDaily_Manual> newReadings)
    {
        foreach (var electricity in newReadings)
        {
            meter.ElectricityDaily_Manual.Add(electricity);
        }
        db.SaveChanges();

    }

    //public static List<DailyGas> CreateGasReadings(DateTime readingDate, decimal cubMTotal)
    //{
    //    List<DailyGas> newReadings = new List<DailyGas>();
    //    var db = new WeatherDbDataContext();
    //    var Gas = db.DailyGas.OrderByDescending(dg => dg.dg_dateTime);
    //    var firstGas = Gas.FirstOrDefault();

    //    DateTime lastReadingDate = firstGas != null ? firstGas.dg_dateTime : readingDate.AddDays(-1);
    //    decimal lastCubMTotal = firstGas != null ? firstGas.dg_cubicMTotal.Value : 0;



    //    int readingDays = (int)readingDate.Subtract(lastReadingDate).TotalDays;

    //    decimal gasKwhConversion = (decimal)11;
    //    decimal cubMIncrease = cubMTotal - lastCubMTotal;
    //    decimal dailyCubM = cubMIncrease/readingDays;
    //    decimal lastKwhTotal = lastCubMTotal * gasKwhConversion;
    //    decimal kwhIncrease = cubMIncrease * gasKwhConversion;
    //    decimal dailyKwh = dailyCubM * gasKwhConversion;
    //    decimal gasCo2Conversion = (decimal)0.185;
    //    decimal dailyCo2 = dailyKwh * gasCo2Conversion;

    //    while (lastReadingDate < readingDate)
    //    {
    //        lastReadingDate = lastReadingDate.AddDays(1);
    //        lastCubMTotal += dailyCubM;
    //        lastKwhTotal += dailyKwh;
    //        var newReading = new DailyGas
    //        {
    //            dg_dateTime = lastReadingDate,
    //            dg_CO2 = dailyCo2,
    //            dg_kWhDaily = dailyKwh,
    //            dg_kWhTotal = lastKwhTotal,
    //            dg_cubicMDaily = dailyCubM,
    //            dg_cubicMTotal = lastCubMTotal
    //        };
    //        newReadings.Add(newReading);

    //    }
    //    return newReadings;
    //}


    //public static void SaveNewGasReadings(List<DailyGas> newReadings)
    //{
    //    var db = new WeatherDbDataContext();
    //    db.DailyGas.InsertAllOnSubmit(newReadings);
    //    db.SubmitChanges();

    //}

}

